<template>
  <basic-container>
    <avue-crud ref="crud"
               :option="option"
               v-model:page="page"
               :table-loading="loading"
               @on-load="onLoad"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               v-model="form"
               :data="data">
      <template #addBtn-form="{}">
        <div style="text-align: center">
          <el-button type="success" @click="handleAdd">添加</el-button>
        </div>
      </template>
      <template #batchList-form="{}">
        <avue-crud :option="batchListOption"
                   :data="form.list"></avue-crud>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script >
import {addGlue, delGlue, listGlue, updateGlue} from "/@/api/produce/glue";
import {reactive, ref, toRefs} from "vue";
import useCurrentInstance from "/@/utils/useCurrentInstance";
import {parseTime} from "../../utils/common";
export default {
  name: "glue",

  setup(props){
    const { proxy } = useCurrentInstance();

    const option = ref({
      searchMenuSpan: 6,
      tip: false,
      border: true,
      index: false,
      viewBtn: false,
      selection: true,
      addBtn:true,
      searchLabelWidth:100,
      excelBtn:false,
      labelWidth:110,
      column: [
        {
          label: '胶水编码',
          prop: "encode",
          miniWidth: 150,
        },

        {
          label: '胶水名称',
          prop: "name",
          miniWidth: 150,
        },
        {
          label: '批次号',
          prop: "batchNumber", search: true,
          miniWidth: 150,hide: true
        },
        {
          label: '生产日期',
          prop: "createTime", minWidth: 200,
          format: 'YYYY-MM-DD HH:mm:ss',
          type: 'date', display: false, hide: true,
          search:true,searchRange:true,
          searchLabelWidth: 120,
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
        },
        {
          label: '生产日期',
          prop: "productionTime",
          format: 'YYYY-MM-DD',
          type: 'date',
          value:  new Date(),
          miniWidth: 150,hide: true
        },
        {
          label: '',
          labelWidth:0,
          prop: 'addBtn',
          span:24,hide: true
        },
        {
          label: '已添加的批次',
          prop: 'batchList',
          span:24,hide: true
        },
        {
          label: '操作人',
          prop: "createUserId",
          miniWidth: 150,  display:false,
        },

        {
          label: '操作时间',
          prop: "updateTime", minWidth: 200,
          format: 'YYYY-MM-DD HH:mm:ss',
          type: 'date', display: false,
          search:true,searchRange:true,
          searchLabelWidth: 120,
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
        },

      ]
    });

    const batchListOption = ref({
      menu: false,
      border:true,
      header:false,
      height: 245,
      align: 'center',
      index: true,
      indexLabel: '序号',
      indexWidth: 150,
      column: [

        {
          label: '批次号',
          prop: "batchNumber",   minWidth: 150,
        },
        {
          label: '时间',
          prop: "productionTime",   minWidth: 150,
        }
      ]
    })

    const loading = ref(true);
    const data = ref([]);
    const crud = ref();
    const selectionList = ref([]);
    const queryParams = ref({});
    const form = ref({list:[]});
    const page = ref({
      pageSize: 10,
      currentPage: 1,
      total: 0

    })
    const onLoad = (pages, params = {}) => {
      loading.value = true;
      listGlue(Object.assign({ page: pages.currentPage,
        limit: pages.pageSize,},params, queryParams.value)).then(res => {
        const resData =  res.data;
        page.value.total = resData.total;
        data.value = resData.list;
        loading.value = false;
        selectionClear();
      })
    }
    const selectionClear = () => {
      selectionList.value = [];
    }
    const rowSave = (row, done, loading) => {
      addGlue(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowUpdate = (row, index, done, loading) => {
      updateGlue(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowDel = (row) => {
      proxy.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return delGlue(row);
          })
          .then(() => {
            onLoad(page.value);
            proxy.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    }
    const refreshChange = () => {
      onLoad(page.value, queryParams.value);
    }
    const searchChange = (params, done) => {
      page.currentPage = 1;
      onLoad(page.value, params);
      done();
    }
    const handleAdd = () => {
        if (!form.value.batchNumber ){
          proxy.$message({
            type: "error",
            message: "请输入批次号!"
          });
          return;
        }
      if (!form.value.productionTime ){
        proxy.$message({
          type: "error",
          message: "请选择生产日期!"
        });
        return;
      }
      if (!Array.isArray(form.value.list)){
        form.value.list = []
      }
      form.value.list.push({
        batchNumber:form.value.batchNumber,
        productionTime:parseTime(form.value.productionTime,'{y}-{m}-{d}'),
      })


    }

    return {
      option,
      batchListOption,
      loading,
      data,
      page,
      form,
      queryParams,
      onLoad,
      rowSave,
      rowUpdate,
      rowDel,
      refreshChange,
      searchChange,
      handleAdd
    }
  }
}
</script>
